package com.kaibai.dao.impl;

import com.kaibai.dao.BaseDao;
import com.kaibai.dao.CartDao;
import com.kaibai.pojo.CartItemDB;

import java.sql.Connection;
import java.util.List;

public class CartDaoImpl extends BaseDao implements CartDao {
    @Override
    public void addCartItemDB(Connection conn, CartItemDB cartItemDB) {
        String sql = "insert into cart_item(uId , gId , name , count , price) values(?,?,?,?,?)";
        update(conn,sql,cartItemDB.getuId(),cartItemDB.getgId(),cartItemDB.getName(),cartItemDB.getCount(),cartItemDB.getPrice());
    }

    @Override
    public List<CartItemDB> getListByUid(Connection conn, Integer uid) {
        String sql = "select id,uId,gId,name,count,price from cart_item where uId=?";
        return getForList(conn,CartItemDB.class,sql,uid);
    }

    @Override
    public int updateItemDB(Connection conn, int gid, int count,int uid) {
        String sql = "update cart_item set count=? where gId=? and uId=?";
        return update(conn,sql,count,gid,uid);
    }

    @Override
    public CartItemDB existItemById(Connection conn, int gid,int uid) {
        String sql = "select id,uId,gId,name,count,price from cart_item where gId=? and uId=?";
        return getInstance(conn,CartItemDB.class,sql,gid,uid);
    }

    @Override
    public int deleteItemDB(Connection conn, int gid, int uid) {
        String sql = "delete from cart_item where gId=? and uId=?";
        return update(conn,sql,gid,uid);
    }

    @Override
    public int deleteItemDBByUid(Connection conn, int uid) {
        String sql = "delete from cart_item where uId=?";
        return update(conn,sql,uid);
    }
}
